<?php
namespace Admin\Controller;
class GroupController extends BaseController{
    function index(){
        if(IS_AJAX){
            $data=M('group')->select();
            foreach ($data as &$v){
                $v['ctime']=date('Y-m-d',$v['ctime']);
                $v['utime']=date('Y-m-d',$v['utime']);
            }
            $this->ajaxReturn($data);
            die;
        }
        $this->display();
        
    }
    
    function add(){   
        if(IS_POST){
            $title=I('post.title','');
            if(!empty($title)){
                $_POST['level']=implode(',',I('post.actions'));
                $_POST['ctime']=time();
                $id=M('group')->add(I('post.'));
                if($id){
                    $this->redirect('index');
                }else{
                    $this->error('添加失败',U(),5);
                }
            }else{
                $this->error('角色名不能为空',u(),5);
            }
            die;
            //dump(I('post.'));
        }
        
        $actions=M('action')->getField('id,title');
        //dump($actions);die;
        $this->assign('actions',$actions);
        $this->display();
        
        
    }
    
    function edit($id=0){
        if($id){
            $row=M('group')->find($id);
            if($row){
                if(IS_POST){
                    $_POST['level']=implode(',',I('post.actions'));
                    $_POST['utime']=time();
                    $id=M('group')->where(array('id'=>$id))->save(I('post.'));
                    if($id){
                        $this->redirect('index');
                    }else{
                        $this->error('修改失败',U(),5);
                    }
                }
                $actions=M('action')->getField('id,title');
                $this->assign('actions',$actions);
                $this->assign('row',$row); 
                $this->display();
                
            }else{
                $this->error('没有找到这行，可能已经删除了',U('index'),5);
            }
           
        }else{
                $this->error('ID不合法',U('index'),5);
            }
        
    }
    
    function del(){
    
    
    
    
    }
    
    
}